<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="func" uri="http://www.zz.com/tag"
%><%@ tag pageEncoding="UTF-8"
%><%@ attribute name="keyFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="valueFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="tableName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="custInspFlag" required="true" rtexprvalue="true" description=""
%><%@ attribute name="relateKeyFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="relateValueFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="defaultKey" required="true" rtexprvalue="true" description=""
%><%@ attribute name="defaultValue" required="true" rtexprvalue="true" description=""
%><%--境内目的地 关联分离 添加标志--%><%@ attribute name="relatation" required="false" rtexprvalue="true" description=""
%><c:set var="basePath" value="${pageContext.request.contextPath}"/>
<c:set var="defaultValue" value="${empty defaultValue ? func:dictName(tableName, defaultKey, (custInspFlag eq 'C' ? 'CODE_C':'CODE_I'),(custInspFlag eq 'C' ? 'NAME_C':'NAME_I')) : defaultValue}"/>


<input type="text" value="${defaultValue}" id="${valueFieldName}" name="${valueFieldName}">
<input type="hidden" value="${defaultKey}" id="${keyFieldName}" name="${keyFieldName}" custInspFlag="${custInspFlag}">
<script>
    function ${valueFieldName}IsSingleDict()
    {
        return '${relateValueFieldName}' == '' || $("#${relateValueFieldName}").size()<1 || $("#${relateValueFieldName}_input").is(":hidden");
    }
    var _split = "------===";
    $(document).ready(function () {
        var txt${valueFieldName} = $("#${valueFieldName}").tautocomplete({
            relateKey:"${keyFieldName}",
            placeholder:"请输入关键字",
            width: "400px",
            columnsCallback: function(){
                if(${valueFieldName}IsSingleDict())
                {
                    if(${'C' eq custInspFlag})
                    {
                        return ['关编码','关名称'];
                    }

                    if(${'I' eq custInspFlag})
                    {
                        return ['检名称','检编码'];
                    }
                }
                return ['关编码','关名称','检名称','检编码'];
            },
            regex: "^[a-zA-Z0-9\\s]+$",
            ajax: {
                url: "${basePath}/cache/getDictParam",
                type: "POST",
                data: function () {
                    var searchData = txt${valueFieldName}.searchdata();
                    return {"name_startsWith": searchData,
                            tableName:'${tableName}',
                            custInspFlag: '${custInspFlag}',
                            isSingleDict: ${valueFieldName}IsSingleDict()
                    };
                },
                success: function (jsonData) {
                    var filterData = [];
                    $.each(jsonData, function (i, v) {
                        var obj = new Object();

                        if(${valueFieldName}IsSingleDict())
                        {
                            if(${'C' eq custInspFlag})
                            {
                                obj.id = v.CODE_C + _split + v.NAME_C;

                                obj.CODE_C = v.CODE_C;
                                obj.NAME_C = v.NAME_C;

                                filterData.push(obj);
                                return;
                            }

                            if(${'I' eq custInspFlag})
                            {
                                obj.id = v.CODE_I + _split + v.NAME_I;

                                obj.CODE_I = v.CODE_I;
                                obj.NAME_I = v.NAME_I;

                                filterData.push(obj);
                                return;
                            }
                        }

                        obj.id = v.CODE_C + _split + v.NAME_C + _split + v.CODE_I + _split + v.NAME_I;

                        obj.CODE_C = v.CODE_C;
                        obj.NAME_C = v.NAME_C;

                        obj.CODE_I = v.CODE_I;
                        obj.NAME_I = v.NAME_I;


                        filterData.push(obj);
                    });
                    return filterData;
                }
            },
            onchange: function () {
                var el = txt${valueFieldName};
                var datas = txt${valueFieldName}.id().split(_split);

                if(${valueFieldName}IsSingleDict())
                {
                    var code = datas[0] || '';
                    var name = datas[1] || '';
                    $("#${keyFieldName}").val(code);
                    $("#${valueFieldName}").val(name);

                    el.ddTextbox.data("id", code);
                    var displayTxt = code + "/" + name;
                    displayTxt = displayTxt == '/' ? '' : displayTxt;
                    el.ddTextbox.data("text", displayTxt);
                    el.ddTextbox.val(displayTxt);

                    return;
                }


                var codeC = datas[0] || '';
                var codeI = datas[2] || '';
                var nameC = datas[1] || '';
                var nameI = datas[3] || '';

                if(${'C' eq custInspFlag})
                {
                    $("#${keyFieldName}").val(codeC);
                    $("#${valueFieldName}").val(nameC);

                    el.ddTextbox.data("id", codeC);
                    var displayTxt = codeC + "/" + nameC;
                    displayTxt = displayTxt == '/' ? '' : displayTxt;
                    el.ddTextbox.data("text", displayTxt);
                    el.ddTextbox.val(displayTxt);

                    //境内目的地进行 关联特殊处理
                    if('DISTRICT'=='${relatation}' && ''!= $.trim($("#${relateKeyFieldName}").val())){

                    }
                    else{
                        if(${not empty relateKeyFieldName} && codeI != '')
                        {
                            $("#${relateKeyFieldName}").val(codeI);
                            if(typeof  _validator != 'undefined' && _validator)
                            {
                                _validator.element($("#${relateKeyFieldName}"))
                            }
                        }

                        if(${not empty relateValueFieldName} && nameI !='')
                        {
                            $("#${relateValueFieldName}").val(nameI);

                            var displayTxt = codeI + "/" + nameI;
                            displayTxt = displayTxt == '/' ? '' : displayTxt;
                            $("#${relateValueFieldName}").closest("div")
                                    .find('input[autocomplete="off"]')
                                    .data("text",displayTxt)
                                    .val(displayTxt);
                        }
                    }
                    return;
                }
                if(${'I' eq custInspFlag})
                {
                    $("#${keyFieldName}").val(codeI);
                    $("#${valueFieldName}").val(nameI);

                    el.ddTextbox.data("id", codeI);
                    var displayTxt = codeI + "/" + nameI;
                    displayTxt = displayTxt == '/' ? '' : displayTxt;
                    el.ddTextbox.data("text", displayTxt);
                    el.ddTextbox.val(displayTxt);
                    //orginalTextBox.val(codeI + '#$#' + nameI);
                    //境内目的地进行 关联特殊处理
                    if('DISTRICT'=='${relatation}' && ''!= $.trim($("#${relateKeyFieldName}").val())){}else{

                        if(${not empty relateKeyFieldName} && codeC != '')
                        {
                            $("#${relateKeyFieldName}").val(codeC);
                            if(typeof  _validator != 'undefined' && _validator)
                            {
                                _validator.element($("#${relateKeyFieldName}"))
                            }
                        }

                        if(${not empty relateValueFieldName} && nameC !='')
                        {
                            $("#${relateValueFieldName}").val(nameC);

                            var displayTxt = codeC + "/" + nameC;
                            displayTxt = displayTxt == '/' ? '' : displayTxt;
                            $("#${relateValueFieldName}").closest("div")
                                    .find('input[autocomplete="off"]')
                                    .data("text",displayTxt)
                                    .val(displayTxt);
                        }
                    }
                    return;
                }

                return;
            }
        });

        var displayTxt = '${defaultKey}' + "/" + '${defaultValue}';
        displayTxt = displayTxt == '/' ? '' : displayTxt;
        $("#${valueFieldName}").closest("div")
                .find('input[autocomplete="off"]')
                .data("text", displayTxt)
                .val(displayTxt);

    });

</script>
